﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>供应商退货管理</title>
    <link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding: 10px 15px;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: auto;">退货单名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="returnName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-search" lay-submit lay-filter="search-btn">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 查询
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
                    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batch-delete">批量删除</button>
                </div>
            </script>
            <table id="supplier-returns-table" lay-filter="supplier-returns-table"></table>
            <script type="text/html" id="actions-bar">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
            </script>
        </div>
    </div>
</div>
<script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
layui.use(['table', 'form', 'layer', 'laydate'], function () {
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var $ = layui.jquery;
    table.render({
        elem: '#supplier-returns-table',
        url: '/SupplierReturns/ShowSupplierReturns',
        request: {
            pageName: 'pageIndex',
            limitName: 'pageSize'
        },
        toolbar: '#toolbar',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '刷新',
            layEvent: 'refresh',
            icon: 'layui-icon-refresh'
        }],
        cols: [[
            { type: 'checkbox', fixed: 'left' },
            { field: 'id', title: 'ID', width: 80, sort: true },
            { field: 'returnCode', title: '退货单编号', minWidth: 200 },
            { field: 'returnName', title: '退货单名称', minWidth: 150 },
            { field: 'procurementCode', title: '采购订单编号', minWidth: 150 },
            { field: 'vendor', title: '供应商', minWidth: 120 },
            { field: 'lotNumber', title: '批次号', minWidth: 120 },
            {
                field: 'returnDate', title: '退货日期', minWidth: 150,
                templet: "<div>{{d.returnDate ? layui.util.toDateString(d.returnDate, 'yyyy-MM-dd') : ''}}</div>"
            },
            {
                field: 'documentStatus', title: '单据状态', minWidth: 100,
                templet: function (d) {
                    return d.documentStatus === 0 ? '<span style=\"color: #FFB800;\">草稿</span>' : '<span style=\"color: #5FB878;\">已确定</span>';
                }
            },
            { title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#actions-bar' }
        ]],
        page: true,
        limit: 10,
        limits: [10, 20, 50, 100],
        done: function () {
            $("[data-field = 'id']").css('display', 'none');
        }
    });
    form.on('submit(search-btn)', function (data) {
        table.reload('supplier-returns-table', {
            where: {
                returnName: data.field.returnName
            },
            page: {
                curr: 1
            }
        });
        return false;
    });
    table.on('toolbar(supplier-returns-table)', function (obj) {
        switch (obj.event) {
            case 'add':
                layer.open({
                    type: 2,
                    title: '添加供应商退货单',
                    area: ['700px', '600px'],
                    content: '/SupplierReturns/SupplierReturnsEditView',
                    end: function(){
                        table.reload('supplier-returns-table');
                    }
                });
                break;
            case 'batch-delete':
                var checkStatus = table.checkStatus('supplier-returns-table');
                var data = checkStatus.data;
                if (data.length === 0) {
                    layer.msg('请选择要删除的行');
                    return;
                }
                var ids = data.map(function(item) {
                    return item.id;
                });
                layer.confirm('确定删除选中的 ' + data.length + ' 条记录吗？', function(index) {
                    deleteRecords(ids);
                    layer.close(index);
                });
                break;
            case 'refresh':
                table.reload('supplier-returns-table', { page: { curr: 1 } });
                break;
        }
    });
    table.on('tool(supplier-returns-table)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: '编辑供应商退货单',
                area: ['700px', '600px'],
                content: '/SupplierReturns/SupplierReturnsEditView?id=' + data.id,
                end: function(){
                    table.reload('supplier-returns-table');
                }
            });
        } else if (obj.event === 'delete') {
            layer.confirm('确定删除此记录吗？', function (index) {
                deleteRecords([data.id]);
                layer.close(index);
            });
        }
    });
    function deleteRecords(ids) {
        $.ajax({
            url: '/SupplierReturns/DelSupplierReturns',
            type: 'DELETE',
            contentType: 'application/json',
            data: JSON.stringify(ids),
            success: function(res) {
                layer.msg('删除成功');
                table.reload('supplier-returns-table');
            },
            error: function() {
                layer.msg('删除失败');
            }
        });
    }
});
</script>
</body>
</html>
